package com.yuncheng.spcyApi.spcyService;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.entity.SpcySqsxBasic;
import com.yuncheng.spcyApi.entity.SpcySqsxYbmjbxx;
import com.yuncheng.spcyApi.entity.SpcySqsxYbmjh;
import com.yuncheng.spcyApi.service.ISpcySqsxBaiscService;
import com.yuncheng.spcyApi.service.ISpcySqsxXpjlService;
import com.yuncheng.spcyApi.service.ISpcySqsxYbmjbxxService;
import com.yuncheng.spcyApi.service.ISpcySqsxYbmjhService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author hua
 */
@Service
public class SpcyYbmjhService {

    @Resource
    private ISpcySqsxYbmjbxxService spcySqsxYbmjbxxService;

    @Resource
    private ISpcySqsxYbmjhService spcySqsxYbmjhService;

    @Resource
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    private ISpcySqsxXpjlService spcySqsxXpjlService;

    /**汇总检查计划-添加/发布 */
    @Transactional(rollbackFor = Exception.class)
    public void hzjcjhAdd(SpcySqsxYbmjbxx bean, List<SpcySqsxYbmjh> jhList){
        spcySqsxYbmjbxxService.save(bean);

        spcySqsxYbmjhService.saveBatch(jhList);

        spcySqsxBaiscService.updateHzjcjhSqsxByYbmzt();
    }

    /** 将申请事项-列入计划 */
    @Transactional(rollbackFor = Exception.class)
    public void lrbmjhBySqsx(SpcySqsxYbmjh bean, SpcySqsxBasic updateBean, LambdaUpdateWrapper<SpcySqsxBasic> updateWrapper){
        spcySqsxBaiscService.update(updateBean, updateWrapper);

        spcySqsxXpjlService.syncUpdateXpjlGzsjBySqsx(bean.getSqsxid(), bean.getFcykssj(), bean.getFcyjssj());
    }

    /** 发布报名计划 */
    @Transactional(rollbackFor = Exception.class)
    public void fbbmjhjbxx(String id){
        // 更新基本信息
        LambdaUpdateWrapper<SpcySqsxYbmjbxx> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxYbmjbxx::getId, id);

        SpcySqsxYbmjbxx updateBean = new SpcySqsxYbmjbxx();
        updateBean.setFbmzt(SpcyCommonConstant.SQSX_YBMJH_BMZT_BMZ);
        updateBean.setFfbrq(new Date());
        updateBean.setFfbrxm(GetCurrentUserUtils.getCurrentUserName());
        spcySqsxYbmjbxxService.update(updateBean, updateWrapper);

        // 更新明细
        LambdaUpdateWrapper<SpcySqsxYbmjh> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(SpcySqsxYbmjh::getFbmjbxxid, id);

        SpcySqsxYbmjh updateBean1 = new SpcySqsxYbmjh();
        updateBean1.setFbmzt(SpcyCommonConstant.SQSX_YBMJH_BMZT_BMZ);
        updateBean1.setFfbrq(new Date());
        updateBean1.setFfbrxm(GetCurrentUserUtils.getCurrentUserName());
        spcySqsxYbmjhService.update(updateBean1, updateWrapper1);
    }

    /** 作废报名计划 */
    @Transactional(rollbackFor = Exception.class)
    public void zfbmjhjbxx(SpcySqsxYbmjbxx bean){
        // 更新基本信息
        LambdaUpdateWrapper<SpcySqsxYbmjbxx> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxYbmjbxx::getId, bean.getId());

        SpcySqsxYbmjbxx updateBean = new SpcySqsxYbmjbxx();
        updateBean.setFbmzt(SpcyCommonConstant.SQSX_YBMJH_BMZT_YZF);
        updateBean.setFbz(bean.getFbz());
        spcySqsxYbmjbxxService.update(updateBean, updateWrapper);

        // 更新明细
        LambdaUpdateWrapper<SpcySqsxYbmjh> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(SpcySqsxYbmjh::getFbmjbxxid, bean.getId());

        SpcySqsxYbmjh updateBean1 = new SpcySqsxYbmjh();
        updateBean1.setFbmzt(SpcyCommonConstant.SQSX_YBMJH_BMZT_YJS);
        spcySqsxYbmjhService.update(updateBean1, updateWrapper1);
    }

}
